package com.example.onlinemonitoring.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.model.Arcxxsensor;
import com.example.onlinemonitoring.model.Arcxxhistory;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.chartres;
import com.example.onlinemonitoring.model.common.pagebase;
import com.example.onlinemonitoring.query.QueryObject;
import com.example.onlinemonitoring.serviceImpl.ArcxxsensorServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
/**
 * Controller
 * 该部分代码为自动生成，请勿随意修改
 * 如有改动请删除该注释
 * @author little pig page
 * @date 2022-03-28
 */
@Api(value = "消弧选线设备", tags = { "|消弧选线设备|设备接口" })
@RestController
@RequestMapping("/arcsuppressionlineselection")
public class ArcxxsensorController {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    @Autowired
    ArcxxsensorServiceImpl arcxxsensorServiceImpl;
    @ApiOperation(value="修改设备参数", notes= "修改设备参数")
    @ResponseBody
    @RequestMapping(value = "/updateAll", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAll(@RequestBody String jsonParam,
        @RequestParam(value = "stationid", required = false) Integer stationid,
        @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
        @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
        @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object  = JSONObject.parseObject(jsonParam);
        Arcxxsensor record = object.toJavaObject(Arcxxsensor.class);
        MyResult<Integer> m = new MyResult<Integer>();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setDeviceaddr(deviceaddr);
        record.setPointaddr(pointaddr);
        if(arcxxsensorServiceImpl.Update(record,false) >= 1){
            m.setMsg("修改成功");
            m.setCode(0);
        }else{
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }
    @ApiOperation(value="修改设备报警", notes= "修改设备报警")
    @ResponseBody
    @RequestMapping(value = "/updateAlarm", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAlarm(@RequestBody String jsonParam,
        @RequestParam(value = "stationid", required = false) Integer stationid,
        @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
        @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
        @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object  = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        //Arcxxsensor record = object.toJavaObject(Arcxxsensor.class);
        Arcxxsensor record = new Arcxxsensor();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setDeviceaddr(deviceaddr);
        record.setPointaddr(pointaddr);
        if(arcxxsensorServiceImpl.Update(record,false) >= 1){
            m.setMsg("修改成功");
            m.setCode(0);
        }else{
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }
    @ApiOperation(value = "查询实时表数据", notes = "查询实时表数据")
    @ResponseBody
    @GetMapping("/selectSensorData")
    public MyResult<Arcxxsensor> selectSensorData(
    @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "limit", required = false) Integer limit,
        @RequestParam(value = "stationid", required = false) Integer stationid,
        @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
        @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
        @RequestParam(value = "devicegroup", required = false) Integer devicegroup
    ) {
        Arcxxsensor record = new Arcxxsensor();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setPointaddr(pointaddr);
        record.setDevicegroup(devicegroup);
        QueryObject qo = new QueryObject(page,limit);
        return arcxxsensorServiceImpl.getList(record,qo);
    }
    @ApiOperation(value="修改监测点名称", notes= "修改监测点名称")
    @ResponseBody
    @RequestMapping(value = "/updateMonitorName", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateMonitorName(@RequestBody String jsonParam,
        @RequestParam(value = "id", required = false) Long id
    ) {
        JSONObject object  = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Arcxxsensor record = new Arcxxsensor();
        record.setId(id);
        record.setPointname(object.getString("pointname"));
        if( arcxxsensorServiceImpl.Update(record,true) >= 1){
            m.setMsg("修改成功");
            m.setCode(0);
        }else{
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }
    // @ApiOperation(value="修改母线名称", notes= "修改母线名称")
    // @ResponseBody
    // @RequestMapping(value = "/updatePtName", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    // public MyResult<Integer> updatePtName(@RequestBody String jsonParam,
    //     @RequestParam(value = "id", required = false) Long id,
    //     @RequestParam(value = "ptaddr", required = false) Integer ptaddr
    // ) {
    //     JSONObject object  = JSONObject.parseObject(jsonParam);
    //     MyResult<Integer> m = new MyResult<Integer>();
    //     Arcxxsensor record = new Arcxxsensor();
    //     Example example = new Example(Arcxxsensor.class);
    //     Example.Criteria criteria = example.createCriteria();
    //     criteria.andEqualTo("stationid", record.getStationid());
    //     criteria.andEqualTo("devicegroup", record.getDevicegroup());
    //     criteria.andEqualTo("deviceid", record.getId());
    //     criteria.andEqualTo("ptaddr", record.getLastptaddr());
    //     //record.setId(id);
    //     //record.setLastptaddr(ptaddr);
    //     record.setPtname(object.getString("ptname"));
    //     if(arcxxsensorServiceImpl.updateByExampleSelective(record,example) >= 1){
    //     //if( arcxxsensorServiceImpl.Update(record,true) >= 1){
    //         m.setMsg("修改成功");
    //         m.setCode(0);
    //     }else{
    //         m.setMsg("修改失败");
    //         m.setCode(1);
    //     }
    //     return m;
    // }
    @ApiOperation(value = "查询历史表数据", notes = "查询历史表数据")
    @ResponseBody
    @GetMapping("/selectHistoryData")
    public MyResult<Arcxxhistory> selectHistoryData(
    @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount) {
        Arcxxhistory record = new Arcxxhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setPointaddr(pointaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setPagecount(pagecount);
        return arcxxsensorServiceImpl.getHisList(record,pagebase);
    }
    @ApiOperation(value = "查询历史表曲线", notes = "查询历史表曲线")
    @ResponseBody
    @GetMapping("/selectHistoryCurve")
    public MyResult<chartres> selectHistoryCurve(
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount,
            @RequestParam(value = "lstr", required = false) String lstr){
        Arcxxhistory record = new Arcxxhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setPointaddr(pointaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setPagecount(pagecount);
        pagebase.setIsasc(true);
        return arcxxsensorServiceImpl.getHisCurveList(record,lstr,pagebase);
    }
    /**
     * 导出Excel
     */
    @GetMapping("exportToExcel")
    public void exportToExcel(HttpServletResponse response,
            @RequestParam(value = "stationid", required = true) Integer stationid,
            @RequestParam(value = "devicegroup", required = true) Integer devicegroup,
            @RequestParam(value = "deviceaddr", required = true) Integer deviceaddr,
            @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime) throws IOException {
        Arcxxhistory record = new Arcxxhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setPointaddr(pointaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        Workbook wb = arcxxsensorServiceImpl.writeToExcelByList(record, pagebase);
        OutputStream output = response.getOutputStream();
        String fileName = "data.xlsx";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition",
                "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
        wb.write(output);
        output.close();
    }
    /**
     * 导出Excel
     */
    @GetMapping("exportToSensorExcel")
    public void exportToSensorExcel(HttpServletResponse response,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "pointaddr", required = false) Integer pointaddr,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr) throws IOException {
        Arcxxsensor record = new Arcxxsensor();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setPointaddr(pointaddr);
        record.setDevicegroup(devicegroup);
        Workbook wb = arcxxsensorServiceImpl.writeToExcelBySensorList(record);
        OutputStream output = response.getOutputStream();
        String fileName = "data.xlsx";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition",
                "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
        wb.write(output);
        output.close();
    }
}
